package com.peakmain.baselibrary.widget.loading;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.SizeUtils;

/**
 * author ：Peakmain
 * createTime：2019/4/13
 * mail:2726449200@qq.com
 * describe：
 */
public class LoadingView extends RelativeLayout {
    private CircleView mLeftView, mMiddleView, mRightView;
    //距离
    private int mTranslationDistance = 30;
    //时间
    private final long ANIMATION_DRUATION = 350;
    //默认false，只有销毁的时候为true
    private boolean isStopAnimation = false;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mTranslationDistance = SizeUtils.dp2px(mTranslationDistance);
        //设置背景
        setBackgroundColor(Color.WHITE);
        //绘制左边的圆
        mLeftView = getCircleView(context);
        mLeftView.exchangeColor(Color.BLUE);
        //绘制右边的圆
        mRightView = getCircleView(context);
        mRightView.exchangeColor(Color.GREEN);
        //绘制中间的圆
        mMiddleView = getCircleView(context);
        mMiddleView.exchangeColor(Color.RED);
        //添加圆
        addView(mLeftView);
        addView(mRightView);
        addView(mMiddleView);


    }
    public void start(){
        post(new Runnable() {
            @Override
            public void run() {
                // 让布局实例化好之后再去开启动画
                expendAnimation();
            }


        });
    }
    private void expendAnimation() {
        if (isStopAnimation) {
            return;
        }
        //开启左边位移
        ObjectAnimator leftTranslationAnimator = ObjectAnimator.ofFloat(mLeftView, "translationX", 0, -mTranslationDistance);
        //开启右边位移动画
        ObjectAnimator rightTranslatioAnimation = ObjectAnimator.ofFloat(mRightView, "translationX", 0, mTranslationDistance);
        AnimatorSet set = new AnimatorSet();
        set.setDuration(ANIMATION_DRUATION);
        set.playTogether(leftTranslationAnimator, rightTranslatioAnimation);
        set.setInterpolator(new DecelerateInterpolator());
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // 往里面跑
                innerAnimation();
            }
        });
        set.start();
    }

    private void innerAnimation() {
        if (isStopAnimation) {
            return;
        }
        //开启左边位移动画
        ObjectAnimator leftTranslationAnimator = ObjectAnimator.ofFloat(mLeftView, "translationX", -mTranslationDistance, 0);
        //开启右边位移动画
        ObjectAnimator rightTranslatioAnimation = ObjectAnimator.ofFloat(mRightView, "translationX", mTranslationDistance, 0);

        AnimatorSet set = new AnimatorSet();
        set.setDuration(ANIMATION_DRUATION);
        set.playTogether(leftTranslationAnimator, rightTranslatioAnimation);
        set.setInterpolator(new AccelerateInterpolator());
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // 往外面跑
                // 切换颜色顺序  左边的给中间 中间的给右边  右边的给左边
                int mLeftViewColor = mLeftView.getColor();
                int mRightViewColor = mRightView.getColor();
                int mMiddleViewColor = mMiddleView.getColor();
                //切换三者颜色
                mLeftView.exchangeColor(mRightViewColor);
                mRightView.exchangeColor(mMiddleViewColor);
                mMiddleView.exchangeColor(mLeftViewColor);
                expendAnimation();
            }


        });
        set.start();
    }

    private CircleView getCircleView(Context context) {
        CircleView circleView = new CircleView(context);
        LayoutParams params = new LayoutParams(SizeUtils.dp2px(10),SizeUtils.dp2px(10));
        params.addRule(CENTER_IN_PARENT);
        circleView.setLayoutParams(params);
        return circleView;
    }

    @Override
    public void setVisibility(int visibility) {
        if(visibility==VISIBLE){
            super.setVisibility(visibility);
        }else{
            super.setVisibility(INVISIBLE);
            //清理所有的动画
            //清理所有动画
            mLeftView.clearAnimation();
            mRightView.clearAnimation();
            ViewGroup parent = (ViewGroup) getParent();
            if (parent != null) {
                //父布局中移除当前view
                parent.removeView(mLeftView);
                parent.removeView(mRightView);
                removeAllViews();//移除自己的
            }
            isStopAnimation = true;
        }

    }
}
